<?php
namespace app\kucun\model;

use think\Model;

class ProductStockModel extends Model{

    protected $table = 'kucun_product_stock';
    protected $pk = 'id';


    CONST STOCK_STATUS_IN_SHOP = 1; //  正常在店
    CONST STOCK_STATUS_OUT_SHOP = 2; // 从店铺出库
    CONST STOCK_STATUS_SELE = 3; // 已销售

    CONST STOCK_STATUS_ARR = [
        1 => '在店',
        2 => "已出库",
        3 => "已销售",
    ];


    protected $with = ['shop', 'product', 'user'];
    protected $append = ['status_str'];

    public function shop(){
        return $this->hasOne(ShopModel::class,'id','shop_id');
    }

    public function product(){
        return $this->hasOne(ProductModel::class,'id','product_id');
    }

    public function user(){
        return $this->hasOne(WxWorkUserModel::class,'id','add_uid');
    }


    public function logs(){
        return $this->hasMany(ProductStockLogModel::class, 'stock_id', 'id')->with(['shop', 'user'])->order('id desc');
    }


    public function getStatusStrAttr(){
        return self::STOCK_STATUS_ARR[$this->status] ?? '';
    }

}